//
//  MainViewController.m
//  OC-新浪微博
//
//  Created by Apple on 15/11/7.
//  Copyright © 2015年 Apple. All rights reserved.
//

#import "MainViewController.h"
#import "HomeTableViewController.h"
#import "MessageTableViewController.h"
#import "DiscoverTableViewController.h"
#import "ProfileTableViewController.h"
#import "MainNaVViewController.h"
#import "UserAccountViewModel.h"
#import "OAuthViewController.h"
#import "ComposeViewController.h"
@interface MainViewController ()
@property (nonatomic, strong) UIButton *btn;
@end

@implementation MainViewController
//懒加载
-(UIButton *)btn {
    
    if (_btn == nil) {
        _btn = [UIButton setBackImageName:@"tabbar_compose_button" ImageName:@"tabbar_compose_icon_add"];
    }
    return _btn;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //添加子控制器
    [self addChildVc];
   //添加撰写按钮
    [self addButton];
   }
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //移动按钮到前面
    [self.tabBar bringSubviewToFront:self.btn];
}
//MARK: - 按钮
- (void)addButton {
   
    
    [self.tabBar addSubview:self.btn];
  //计算frame
    NSUInteger count = self.childViewControllers.count;
    //解决手指触摸问题
    int w = self.tabBar.bounds.size.width / count -1;
    self.btn.frame = CGRectInset(self.tabBar.bounds, 2 * w, 0);
    [self.btn addTarget:self action:@selector(clickComposedButton) forControlEvents:UIControlEventTouchUpInside];
}
//MARK: - 添加子控制器
- (void)addChildVc {
//首页
    [self addChildVc:[HomeTableViewController new] title:@"首页" ImageName:@"tabbar_home"];
//信息
    [self addChildVc:[MessageTableViewController new] title:@"信息" ImageName:@"tabbar_message_center"];
//空白控制器
    [self addChildVc:[UIViewController new] title:nil ImageName:nil];
//发现
    [self addChildVc:[DiscoverTableViewController new] title:@"发现" ImageName:@"tabbar_discover"];
//我
    [self addChildVc:[ProfileTableViewController new] title:@"我的设置" ImageName:@"tabbar_profile"];
}
- (void)addChildVc:(UIViewController *)Vc title:(NSString *)title  ImageName:(NSString *)ImageName {
    if (title != nil && ![title  isEqual: @""]) {
        Vc.title = title;
    }
    if (ImageName != nil && ![ImageName  isEqual: @""]) {
        
        Vc.tabBarItem.image = [UIImage imageNamed:ImageName];
    }
    MainNaVViewController *nav = [[MainNaVViewController alloc]initWithRootViewController:Vc];
    [self addChildViewController:nav];
}
#pragma mark /*******************  撰写按钮点击事件  *******************/
- (void)clickComposedButton {
// 判断用户是否登录
    UIViewController *vc = [UIViewController new];
    if ([UserAccountViewModel shardViewModel].userlogin) {
        vc = [ComposeViewController new];
    }else {
    
        vc = [OAuthViewController new];
    }
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
    [self presentViewController:nav animated:YES completion:nil];
}
@end
